<template>
	<div>
		<el-form ref="form" :model="form" label-width="80px">
			<el-form-item label="商品名称:">
			    <el-input  disabled v-model="form.goods_name" size="small"></el-input>
			</el-form-item>
			<div class="flex">
				<el-form-item label="应扫数量:">
				    <el-input style="width: 150px;" disabled v-model="form.goods_number" size="small"></el-input>
				</el-form-item>
				<el-form-item label="已扫数量:">
				    <el-input style="width: 150px;" disabled v-model="form.codeArr.length" size="small"></el-input>
				</el-form-item>
			</div>
			<el-form-item label="扫描:" v-if="ttype==1">
				<!-- @change="pushCode" -->
				 <el-input style="width: 245px;" 
					@keyup.enter.native="pushCode"
					ref="changeCode"
					:disabled="form.goods_number == form.codeArr.length"  v-model="form.code" size="small"></el-input>
				 <el-button style="margin-left: 10px;" type="text" @click="createCode">自动生成</el-button>
				 <!-- <select v-model="codeLength" id="codeLength">
					 <option :value="el" v-for="el in 18">{{el}}</option>
				 </select>
				 位 -->
			</el-form-item>
			<el-form-item label="扫描:" v-if="ttype==2">
				<!-- @change="pushCode2" -->
				 <el-input style="width: 245px;" 
					@keyup.enter.native="pushCode2"
					ref="changeCode"
					:disabled="form.goods_number == form.codeArr.length"  v-model="form.code" size="small"></el-input>
			</el-form-item>
			<el-form-item label="串号:">
				<div class="flex">
					<div class="inputBox" v-if="ttype==1">
						 <el-input style="width: 200px;" 
							:rows="10"
							v-model="code_text"
							type="textarea"></el-input>
					</div>
					<div style="margin: 0 10px;" v-if="ttype==1">
						<el-button size="mini" type="primary"
							@click="insertDD"
							>导入</el-button>
					</div>
					<div>
						<div v-for="(el,i) in form.codeArr">
							{{el}} 
							<i class="el-icon-delete" 
								v-if="!rowData.id"
								@click="delCode(i,el)" style="cursor: pointer;"></i>
						</div>
						<div v-if="!form.codeArr.length">
							--
						</div>
					</div>
				</div>
				
			</el-form-item>
		</el-form>	
	</div>
</template>

<script>
	export default {
		props:{
			rowData:{
				type:Object,
				default:{}
			},
			ttype:{
				type:Number,
				default:1
			},
			list1:{
				type:Array,
				default:[]
			}
		},
		mounted() {
			this.$refs.changeCode.focus()
			if(this.rowData.codeArr){
				this.form.codeArr = [...this.rowData.codeArr]
			}else{
				this.form.codeArr = [...this.rowData.codeArray]
			}
			this.form.goods_number = this.rowData.goods_number
			this.form.goods_name = this.rowData.goods_name
		},
		data(){
			return {
				form:{
					codeArr:[],
					goods_number:1,
					goods_name:"",
					code:""
				},
				codeLength:16,
				code_text:''
			}
		},
		methods:{
			insertDD(){
				
				var oparray = [];
				var res = this.code_text
				res = res.replace(/^\n*/, "");
				res = res.replace(/\n{2,}/g, "\n");
				res = res.replace(/\n*$/, "");
				oparray = res.split("\n");
				
				if(oparray.length){
					this.pushCode3(oparray)
				}
			},
			pushCode3(oparray){
				//检查库存中有没有这个商品编码 有的话不让添加
				let params = {
					code:oparray,
					goods_name:this.form.goods_name
				}
				this.$post('/buyerSys/checkGoodsForCode',params,res=>{
					let result = res.data
					if(result.code==200){
						
						for(let code of oparray){
							if(code.length != this.rowData.number_length){
								return this.$message.error('商品串号和设定编码长度不一致') 
							}
							
							// 检查编码有没有在当前数组中
							for(let item of this.form.codeArr){
								if(item.toUpperCase() == code.toUpperCase()){
									return this.$message.error('不能录入相同串号')
								}
							}
							
							//检查编码有没有在列表中
										
							for(let item of this.list1){
								for(let ele of item.codeArr){
									if(ele.toUpperCase() == code.toUpperCase()){
										return this.$message.error('不能录入相同串号')
									}
								}
							}
						}
						
						this.form.codeArr.push(...oparray)
						this.$emit('getCode',this.form.codeArr)
						this.$refs.changeCode.focus()
					}
				})
			},
			
			pushCode(){
				if(!this.$verify.isValidString(this.form.code)){
					this.form.code = ''
					return this.$message.error('商品串号格式有误') 
				}
				if(this.form.code.length != this.rowData.number_length){
					return this.$message.error('商品串号和设定编码长度不一致') 
				}
				
				// 检查编码有没有在当前数组中
				for(let item of this.form.codeArr){
					if(item.toUpperCase() == this.form.code.toUpperCase()){
						return this.$message.error('不能录入相同串号')
					}
				}
				
				//检查编码有没有在列表中
			
				for(let item of this.list1){
					for(let ele of item.codeArr){
						if(ele.toUpperCase() == this.form.code.toUpperCase()){
							return this.$message.error('不能录入相同串号')
						}
					}
				}
				
				
				
				
				
				//检查库存中有没有这个商品编码 有的话不让添加
				this.$post('/buyerSys/checkGoodsForCode',{code:[this.form.code]},res=>{
					let result = res.data
					if(result.code==200){
						this.form.codeArr.push(this.form.code)
						this.form.code = ''
						this.$emit('getCode',this.form.codeArr)
						this.$refs.changeCode.focus()
					}
				})
				
			},
			pushCode2(){
				// 检查编码有没有在当前数组中
				for(let item of this.form.codeArr){
					if(item.toUpperCase() == this.form.code.toUpperCase()){
						return this.$message.error('不能录入相同串号')
					}
				}
				
				//检查编码有没有在列表中
				
				for(let item of this.list1){
					for(let ele of item.codeArray){
						if(ele.toUpperCase() == this.form.code.toUpperCase()){
							return this.$message.error('不能录入相同串号')
						}
					}
				}
				
				//检查库存中有没有这个商品编码 有的话不让添加
				let params = {
					code:this.form.code,
					goods_name:this.form.goods_name
				}
				this.$post('/buyerSys/checkGoodsForInventory',params,res=>{
					let result = res.data
					if(result.code==200){
						this.form.codeArr.push(this.form.code)
						this.form.code = ''
						this.$emit('getCode',this.form.codeArr)
						this.$refs.changeCode.focus()
					}
				})
				
			},
			delCode(i,el){
				
				if(!this.rowData.id){
					this.form.codeArr.splice(i,1)
					this.$emit('getCode',this.form.codeArr)
					setTimeout(()=>{
						this.$refs.changeCode.focus()
					},100)
				}
				// let params = {
				// 	code:el,
				// 	goods_name:this.form.goods_name,
				// 	type:this.ttype
				// }
				// this.$post('/buyerSysUpdate/delOneInventory',params,res=>{
				// 	let result = res.data
				// 	if(result.code==200){
				// 		this.form.codeArr.splice(i,1)
				// 		this.$emit('getCode',this.form.codeArr)
				// 		setTimeout(()=>{
				// 			this.$refs.changeCode.focus()
				// 		},100)
				// 	}
				// })
			},
			createCode(){
				this.form.codeArr = []
				for(let i=0;i<this.rowData.goods_number;i++){
					this.createCodeConfirm()
				}
			},
			createCodeConfirm(){
				if(this.rowData.number_length){
					this.codeLength=this.rowData.number_length
				}
				let num = this.$utils.getRamNumber(this.codeLength)
				
				
				
				if(this.form.codeArr.length<this.form.goods_number){
					// if(num != this.rowData.number_length){
					// 	return this.$message.error('商品串号和设定编码长度不一直') 
					// }
					this.form.codeArr.push(num)
					this.$emit('getCode',this.form.codeArr)
					this.$refs.changeCode.focus()
				}
			},
		}
	}
</script>
<style lang="scss" scoped>
	#codeLength{
		border: 0px;
		outline: 1px solid rgb(204,204,204);
		margin-left: 5px;
	}
</style>